package com.ede.roverhu.simplebackstage.Activities;


import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ede.roverhu.simplebackstage.Constant.HandlerConstant;
import com.ede.roverhu.simplebackstage.Domain.VcodeMessageDomain;
import com.ede.roverhu.simplebackstage.Http.HttpClient;
import com.ede.roverhu.simplebackstage.R;
import com.ede.roverhu.simplebackstage.Utils.IntentUtils;
import com.ede.roverhu.simplebackstage.Utils.LogUtils;
import com.ede.roverhu.simplebackstage.Utils.ToastUtils;


public class LoginActivity extends AppCompatActivity {


    private EditText mEditText4UserName;
    private EditText mEditText4PassWord;
    private EditText mEditText4Vcode;
    private ImageView mImageView4Vcode;
    private Button mButton4Login;
    private Button mButton4Bluetooth;
    public Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            switch (msg.what) {
                case HandlerConstant.LOGIN_ERROR:
                    ToastUtils.show(getApplicationContext(), msg.obj.toString() + "");
                    break;
                case HandlerConstant.LOGIN_FAIL:
                    ToastUtils.show(getApplicationContext(), "登录失败");
                    break;
                case HandlerConstant.LOGIN_SUCCESS:
                    ToastUtils.show(getApplicationContext(), "登录成功");
                    IntentUtils.startActivityAndFinish(LoginActivity.this, MainActivity.class);
                    break;
                case HandlerConstant.VCODE_SUCCESS:
                    VcodeMessageDomain v = (VcodeMessageDomain) msg.obj;
                    LogUtils.e(v.phpsessid);
                    mImageView4Vcode.setImageDrawable(v.drawable);
                    break;
                case HandlerConstant.VCODE_FAIL:
                    ToastUtils.show(getApplicationContext(), "获取验证码失败");
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();

    }

    //init
    private void init() {
        mButton4Bluetooth = (Button) findViewById(R.id.bt_login_bluetooth);
        mEditText4UserName = (EditText) findViewById(R.id.et_login_username);
        mEditText4PassWord = (EditText) findViewById(R.id.et_login_password);
        mEditText4Vcode = (EditText) findViewById(R.id.et_login_vcode);
        mImageView4Vcode = (ImageView) findViewById(R.id.iv_login_vcode);
        mButton4Login = (Button) findViewById(R.id.bt_login_login);
        HttpClient.getVcodeImg(mhandler, LoginActivity.this);
        mButton4Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mEditText4UserName.getText().toString();
                String password = mEditText4PassWord.getText().toString();
                String vcode = mEditText4Vcode.getText().toString();
                HttpClient.logIn(mhandler, username, password, vcode, LoginActivity.this);
            }
        });

        mImageView4Vcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpClient.getVcodeImg(mhandler, LoginActivity.this);
            }
        });

        mButton4Bluetooth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentUtils.startActivityAndFinish(LoginActivity.this, BlueToothActivity.class);
            }
        });
    }


}
